package com.hanamaki.dataStructDemo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapTest {

    public static void main(String[] args) {

        System.out.println("定义一个存放 key是Integer类型   value是String类型的");
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(1, "hanamaki");
        map1.put(2, "hanamaki2");

        // 打印map集合
        System.out.println(map1);

        String s = map1.get(1);
        System.out.println(s);

        System.out.println(map1.get("2"));


        System.out.println("定义一个存放key是 String   value是String类型的");
        Map<String, String> map2 = new HashMap<>();
        map2.put("name", "hanamaki");
        map2.put("sex", "男");

        System.out.println(map2.get("name"));


        System.out.println("map的遍历");

        System.out.println("通过KeySet获取map中的key，遍历set获取key  得到 value");
        Set<Integer> setKey = map1.keySet();
        for (Integer key : setKey) {
            System.out.println("当前key为："+key+",值为："+map1.get(key));
        }


        System.out.println("通过entrySet() 遍历得到 map1中的 key和值");
        for (Map.Entry<Integer, String> entry : map1.entrySet()) {
            System.out.println("当前key为："+entry.getKey()+",值为："+entry.getValue());
        }

        System.out.println("通过迭代器 遍历 map2");
        Iterator<Map.Entry<String, String>> iterator = map2.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            System.out.println("当前key为："+next.getKey()+",值为："+next.getValue());
        }
    }
}
